package com.ardf.common;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 自定localtime->String序列化器
 * @author ShiYunHao
 */
public class LocalTimeToStringSerializer extends JsonSerializer<LocalTime> {
    @Override
    public void serialize(LocalTime localTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss.SS");
        // 将 LocalTime 格式化为字符串
        String formattedTime = localTime.format(formatter);
        // 将格式化后的字符串写入 JSON
        jsonGenerator.writeString(formattedTime);
    }
}
